#lang racket

(require racket/serialize)

(define _setthash (hash))

(define (settings-set! key value)
  (set! _setthash (hash-set _setthash key value)))

(define (settings-get key)
  (hash-ref _setthash key))

(define (settings-flush)
  (call-with-output-file (string-append
                          (path->string (find-system-path 'home-dir))
                          "./kirisurf.conf")
    #:exists 'truncate/replace
    (lambda (out)
      (write (serialize _setthash) out))))

(define (settings-initialize!)
  (if (file-exists? (string->path (string-append
                                   (path->string (find-system-path 'home-dir))
                                   "./kirisurf.conf")))
      (with-input-from-file (string-append
                             (path->string (find-system-path 'home-dir))
                             "./kirisurf.conf")
        (λ() (set! _setthash (deserialize (read)))))
      (set! _setthash
            (make-immutable-hash
             `((tunntype . kiri)
               (startbrowser . #t)))))
  (when (equal? (system-type) 'windows)
    (settings-set! 'tunntype 'ssl)))

(provide settings-set!
         settings-get
         settings-flush
         settings-initialize!)